<?php

namespace App\Http\Controllers;

use App\Product;
use App\Type;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CommonController extends Controller
{
    public function getChildren(Request $request)
    {
        $id = $request->input('id');
        $html = '<option value="">--请选择--</option>';
        if (!empty($id)) {
            $models = Type::where('parent_id', '=', $id)->get();
            foreach ($models as $model) {
                $html .= '<option value="' . $model->id . '">' . $model->name . '</option>';
            }
        }
        $return = ['code' => 0, 'msg' => $html];
        return response()->json($return);
    }

    public function getProduct(Request $request)
    {
        $id = $request->input('id');
        $html = '<option value="">--请选择--</option>';
        if (!empty($id)) {
            $products = Product::where('model_id', $id)->get();
            foreach ($products as $product) {
                $html .= '<option value="' . $product->id . '">' . $product->name . '</option>';
            }
        }
        $return = ['code' => 0, 'msg' => $html];
        return response()->json($return);
    }
}